import { list } from '../../../api/groupOrder'
import { list as groupBuyList } from '../../../api/groupBuy'
let app = getApp();
Page({

    data: {
        currentTab:1,
        groupOrderList:[],
        pageNum: 1,
        pageSize: 10,
        loadinging:false,
        refreshing:false,
        refreshTitle:'释放刷新页面...',
        groupBuyName:[
        ],
        clickId:"",
        productType:false
    },
    nameclick(data){
        this.data.pageNum = 1
        this.data.clickId = data.currentTarget.dataset.item.id
        this.setData({
            pageNum:  this.data.pageNum,
            clickId:  this.data.clickId
        })
        this.loadList()
    },
    swichNav(){
        this.data.pageNum=1
        this.data.currentTab=1
        this.setData({
            pageNum:this.data.pageNum,
            currentTab:this.data.currentTab
        })
        this.loadList()
    },
    swichNavagin(){
        this.data.pageNum=1
        this.currentTab=4
        this.setData({
            pageNum: this.data.pageNum,
            currentTab:this.currentTab
        })
        this.loadList()
    },
    loadGroupBuy(){
        groupBuyList({mchId:app.globalData.mchId,isShow:false,type:2}).then(res=>{
            this.data.groupBuyName= res.data.data
            this.setData({
                groupBuyName:this.data.groupBuyName
            })
            if (this.data.groupBuyName.length>0) {

                this.data.clickId = this.data.groupBuyName[0].id;
                this.setData({
                    clickId:this.data.clickId
                })
            }

        })
    },
    formatTime(val) {
        let date = new Date(val);
        let y = date.getFullYear();
        let MM = date.getMonth() + 1;
        MM = MM < 10 ? ('0' + MM) : MM;
        let d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        let h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        let m = date.getMinutes();
        m = m < 10 ? ('0' + m) : m;
        let s = date.getSeconds();
        s = s < 10 ? ('0' + s) : s;
        return y + '-' + MM + '-' + d + " " + h + ":" + m + ":" + s;
},
    loadList(){
        var _this = this
        list({pageNum:_this.data.pageNum,pageSize:_this.data.pageSize,orderStatus:this.data.currentTab}).then(res=>{
            if (this.data.pageNum == 1) {
                this.data.groupOrderList=[]
            }
            res.data.data.forEach(function (item){
                item.createDate = _this.formatTime(item.createDate)
                if (item.itemList.length > 0){
                    item.itemList.forEach(function (items){
                        // items.thumbnail = _this.thumbnail(items.thumbnail,340,340)
                    })
                }
                _this.data.groupOrderList.push(item)
            })
            this.setData({
                groupOrderList:_this.data.groupOrderList,
            })
            if (res.data.data.length < 10) {
                _this.data.productType = true
                this.setData({
                    productType: _this.data.productType,
                })
            }
        })
    },
    onrefresh(event) {
        this.refreshing = true
        this.refreshTitle = '加载中...'
        setTimeout(() => {
            this.pageNum=1
            this.refreshing = false
            this.loadList()
        }, 1000)
    },
    onpullingdown(event) {
        this.refreshTitle = '释放刷新页面...'
    },
    onloading(event) {
        this.loadinging = true
        setTimeout(() => {
            this.loadinging = false
            this.pageNum += 1
            this.loadList()
        }, 1000)
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // this.loadGroupBuy()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.loadList()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        // console.log(this.data.productType,"1231321")
        if(this.data.productType == false){
            wx.showLoading({
                title: '加载中',
                })
                setTimeout(function () {
                wx.hideLoading()
                }, 1000)
            this.data.pageNum += 1
            this.loadList()
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
